﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Code;
using ZFine.Domain.Entity;

namespace ZFine.Application.Project
{
    public class PositionRecordApp
    {
        private IPositionRecordRepository service = new PositionRecordRepository();

        public List<PositionRecordEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<PositionRecordEntity> GetList(Expression<Func<PositionRecordEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }

        public List<PositionRecordEntity> GetList(Pagination pagination)
        {
            var expression = ExtLinq.True<PositionRecordEntity>();
            expression = expression.And(t => 1==1);
            return service.FindList(expression, pagination);
        }
        public PositionRecordEntity GetForm(Expression<Func<PositionRecordEntity, bool>> predicate)
        {
            return service.FindEntityinclude(predicate);
        }
        public PositionRecordEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 添加职位（将以前有效的先删除）
        /// </summary>
        /// <param name="newpositionrecord"></param>
        /// <param name="resultmsg"></param>
        /// <returns></returns>
        public  int submitAddpositionRecord(ref PositionRecordEntity newpositionrecord, ref string resultmsg)
        {
            newpositionrecord.Create();
            var thisadminobj = OperatorProvider.Provider.GetCurrent();
            if (thisadminobj != null)
            {
                newpositionrecord.CreateAdmin = new UserEntity { F_Id = thisadminobj.UserId };
            }
            else
            {
                resultmsg = "当前人未登陆";
                return -2;
            }

            var historyIsExit = service.FindEntity(f => f.State == true);
            DeleteHistoryIsExit(historyIsExit);
            newpositionrecord.State = true;
            return service.AddPostition(newpositionrecord);
        }

        private void DeleteHistoryIsExit(PositionRecordEntity thisent)
        {
            if (thisent != null)
            {
                thisent.State = false;
                service.Update(thisent);
                var historyIsExit = service.FindEntity(f => f.State == true);
                DeleteHistoryIsExit(historyIsExit);
            }
            else
            {
                return;
            }
        }
    }
}
